﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class Main : System.Web.UI.Page
    {
        private ProjectMainManager objProjectMainManager = new ProjectMainManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        int workParentId;

        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            if(!IsPostBack)
            {
                try
                {
                    string strTemp = "";
                    ProjectMain objProjectMian = objProjectMainManager.GetProjectInfo(workParentId,null,"PJMains");
                    if(objProjectMian!=null)
                    {
                        txtPJNo.Text = objProjectMian.PJId;
                        txtPJName.Text = objProjectMian.PJName;
                        txtMold.Text = objProjectMian.Mold;
                        txtMaktx.Text = objProjectMian.Maktx;
                        txtMaktxName.Text = objProjectMian.MaktxName;
                        txtCustomer.Text = objProjectMian.Customer;
                        txtBusiness.Text = objProjectMian.BusinessPlate;
                        txtPM.Text = objProjectMian.PM;
                        ddlPjType.SelectedValue = objProjectMian.PJType;
                        string[] strs = objProjectMian.MoldType.Split(',');
                        for (int i = 0; i < strs.Length; i++)
                        {
                            if (strs[i] == "量产模") { cblMoldType.Items[0].Selected = true; continue; }
                            if (strs[i] == "样品模") { cblMoldType.Items[1].Selected = true; continue; }
                            if (strs[i] == "商品模") { cblMoldType.Items[2].Selected = true; continue; }
                            if (strs[i] == "转移模") { cblMoldType.Items[3].Selected = true; continue; }
                            if (strs[i] == "改模") { cblMoldType.Items[4].Selected = true; continue; }
                            if (strs[i] == "备模") { cblMoldType.Items[5].Selected = true; continue; }
                        }
                        txtData.Text = objProjectMian.Statues == "1" ? "已同步" : "未同步";
                        string startDate = objProjectMian.StartDate.ToString("yyyy-MM-dd");
                        strTemp = ShowDate("txtStartDate", startDate,strTemp);
                        string t1EndDate = objProjectMian.T1EndDate.ToString("yyyy-MM-dd");
                        strTemp = ShowDate("txtT1EndDate", t1EndDate,strTemp);
                        string t2EndDate = objProjectMian.T2EndDate.ToString("yyyy-MM-dd");
                        strTemp = ShowDate("txtT2EndDate", t2EndDate,strTemp);
                        string t3EndDate = objProjectMian.T3EndDate.ToString("yyyy-MM-dd");
                        strTemp = ShowDate("txtT3EndDate", t3EndDate,strTemp);
                        string t4EndDate = objProjectMian.T4EndDate.ToString("yyyy-MM-dd");
                        strTemp = ShowDate("txtT4EndDate", t4EndDate,strTemp);
                        string t5EndDate = objProjectMian.T5EndDate.ToString("yyyy-MM-dd");
                        strTemp = ShowDate("txtT5EndDate", t5EndDate,strTemp);
                        strTemp = string.Format("<script>{0}</script>", strTemp);
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", strTemp);

                    }
                    else
                    {
                        txtPJNo.Text = "";
                        txtPJName.Text = "";
                        txtMold.Text = "";
                        txtMaktx.Text = "";
                        txtMaktxName.Text = "";
                        txtCustomer.Text = "";
                        txtBusiness.Text = "";
                        txtPM.Text = "";
                        ddlPjType.SelectedIndex = -1;
                        cblMoldType.SelectedIndex = -1;
                        txtData.Text = "";
                        strTemp = ShowDate("txtStartDate", "", strTemp);
                        strTemp = ShowDate("txtT1EndDate", "", strTemp);
                        strTemp = ShowDate("txtT2EndDate", "", strTemp);
                        strTemp = ShowDate("txtT3EndDate", "", strTemp);
                        strTemp = ShowDate("txtT4EndDate", "", strTemp);
                        strTemp = ShowDate("txtT5EndDate", "", strTemp);
                    }
                    int pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 6);
                    int statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                    if(pjCount>0 && statusNo<1)
                    {
                        txtPJName.Enabled = true;
                        txtMold.Enabled = true;
                        txtMaktx.Enabled = true;
                        txtMaktxName.Enabled = true;
                        txtCustomer.Enabled = true;
                        txtBusiness.Enabled = true;
                        txtPM.Enabled = true;
                        ddlPjType.Enabled = true;
                        cblMoldType.Enabled = true;
                        BtnSubmit1.Visible = true;
                    }
                    else if((pjCount>0 && statusNo==2))
                    {
                        BtnSubmit2.Visible = true;
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
        }
        private string ShowDate(string id, string date, string strTemp)
        {
            string str = "";
            if (date != "1900-01-01")
            {
                str = strTemp + string.Format("document.getElementById('{0}').value = '{1}';", id, date);
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            return str;

        }


        protected void BtnSubmit1_Click(object sender, EventArgs e)
        {
            string moldType = "";
            for (int i = 0; i < cblMoldType.Items.Count; i++)
            {
                if (cblMoldType.Items[i].Selected)
                {
                    if (moldType.Length == 0)
                    { moldType = cblMoldType.Items[i].Text; }
                    else { moldType = moldType + "," + cblMoldType.Items[i].Text; }
                }
            }
            ProjectMain objProject = new ProjectMain
            {
                WorkParentId = workParentId,
                PJId = txtPJNo.Text.ToString(),
                PJName = txtPJName.Text.ToString().Trim(),
                Mold = txtMold.Text.ToString().Trim().ToUpper(),
                Maktx = txtMaktx.Text.ToString().Trim(),
                MaktxName = txtMaktxName.Text.ToString().Trim(),
                Customer = txtCustomer.Text.ToString().Trim(),
                BusinessPlate = txtBusiness.Text.ToString().Trim(),
                PM = txtPM.Text.ToString().Trim(),
                PJType = ddlPjType.SelectedValue.ToString(),
                MoldType = moldType
            };
            objProject.StartDate = Request.Params.Get("txtStartDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtStartDate"));
            objProject.T1EndDate = Request.Params.Get("txtT1EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT1EndDate"));
            objProject.T2EndDate = Request.Params.Get("txtT2EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT2EndDate"));
            objProject.T3EndDate = Request.Params.Get("txtT3EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT3EndDate"));
            objProject.T4EndDate = Request.Params.Get("txtT4EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT4EndDate"));
            objProject.T5EndDate = Request.Params.Get("txtT5EndDate") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtT5EndDate"));
            try
            {
                string pjId = objProjectMainManager.InsertOrUpdateProjectInfo(objProject, "PJMains");
                string msg = string.Format("<script>alert('项目编号:{0}保存成功！')</script>", pjId);
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void BtnSubmit2_Click(object sender, EventArgs e)
        {
            try
            {
                objProjectMainManager.UpdateProjectInfo(workParentId, "1","PJMains");
                string msg = string.Format("<script>alert('同步成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }
}